package com.travel.Controller;

import com.github.pagehelper.PageInfo;
import com.travel.Bean.Culture;
import com.travel.Bean.Manager;
import com.travel.Service.Impl.CultureServiceImpl;
import com.travel.Service.Impl.ManagerServiceImpl;
import com.travel.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/AdminService")
public class AdminServiceController {

    @Autowired
    private ManagerServiceImpl managerService;
    @Autowired
    private CultureServiceImpl cultureService;


    //获取管理员名称
    public String getUsername() {
        String username;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        return username;
    }

    @ApiOperation("admin查看自己所在的景区的信息")
    @GetMapping("/CulturePage")
    public ModelAndView CulturePage(){
        ModelAndView modelAndView = new ModelAndView();

        int culture_id = managerService.getCultureIdByManagerName(getUsername());
        Culture culture;
        if(culture_id > 0) {
            culture = cultureService.findCultureById(culture_id);
            modelAndView.addObject("type","culture");
            modelAndView.addObject("culture",culture);
            modelAndView.setViewName("public/CultureView");
        }
        else{
            modelAndView.setViewName("public/CultureAdd");
        }

        return modelAndView;
    }

    @ApiOperation("admin管理员信息")
    @GetMapping("/AdminPage")
    public ModelAndView AdminPage(){
        return new ModelAndView("public/AdminPage");
    }

    @ApiOperation("admin管理员信息")
    public R AdminPageList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "15") Integer limit){
        int culture_id = managerService.getCultureIdByManagerName(getUsername());
        if(culture_id < 1){
            return R.no(2);
        }
        PageInfo<Manager> managerPageInfo = managerService.getManagerByCultureId(culture_id,page,limit);
        return R.ok("0",managerPageInfo.getList(),managerPageInfo.getTotal());
    }



}
